package com.gjintel.business.proj.controller;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONObject;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.gjintel.business.proj.model.ProfessorInfo;
import com.gjintel.business.proj.model.ProjectInfo;
import com.gjintel.business.proj.service.IProfessorService;
import com.gjintel.business.proj.service.IProjectInfoService;
import com.gjintel.framework.constant.Constant;
import com.gjintel.framework.constant.ProjectStatus;
import com.gjintel.framework.user.model.User;
import com.gjintel.framework.util.CommUtil;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.workflow.trade.service.IWorkflowProcessInstanceService;


@Controller
@RequestMapping("/business/professor")
public class ProfessorController {
	@Autowired
	private IProfessorService professorService;
	
	@Autowired
	private IWorkflowProcessInstanceService workflowProcessInstanceService;
	
	@Autowired
	private IProjectInfoService projectInfoService;
	
	/**
	 * @description 上传专家信息
	 * @author shy
	 * @date 2016-8-27
	 * @param ProfessorInfo 
	 */
	@RequestMapping("/saveProfessorInfo")
	@ResponseBody
	public String saveProfessorInfo(ProfessorInfo professor,HttpSession session){
		this.professorService.saveProfessorInfo(professor);
		return Constant.SUCCESS;
	}
	
	/**
	 * @description 跳转到专家列表页面
	 * @author shy
	 * @date 2016-8-29
	 */	
	@RequestMapping("/toProfessorListPage")
	@ResponseBody
	public ModelAndView toProfessorListPage(String projectId,HttpSession session){
		ModelAndView mv = new ModelAndView("/proj/leader/professor-list");
		mv.addObject("projectId", projectId);
		System.out.println(projectId);
		return mv;
	}
	
	
	/**
	 * @description 专家列表
	 * @author shy
	 * @date 2016-8-29
	 */	
	
	@RequestMapping("/professorInfoList")
	@ResponseBody
	public void getProfessorInfoList(String projectId, HttpServletResponse resp, Page page){
		this.professorService.getProfessorInfo(projectId,page);	
		JSONObject jsonObject = CommUtil.getPageJSONData(page, true);
		try {
			resp.getWriter().print(jsonObject);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @description 录入专家信息页面
	 * @author shy
	 * @date 2016-8-30
	 */	
	@RequestMapping("/toAddProfessorPage")
	@ResponseBody
	public ModelAndView toAddProfessorPage(String projectId,HttpSession session){
		ModelAndView mv = new ModelAndView("/proj/leader/professor-input");
		mv.addObject("projectId", projectId);
		return mv;
	}
	
	/**
	 * @description 修改专家信息页面
	 * @author shy
	 * @date 2016-8-30
	 */	
	@RequestMapping("/toEditProfessorPage")
	@ResponseBody
	public ModelAndView toEditProfessorPage(String professorId,HttpSession session){
		ProfessorInfo professor = new ProfessorInfo();
		professor=this.professorService.getProfessorInfoById(professorId);
		ModelAndView mv = new ModelAndView("/proj/leader/professor-edit");
		mv.addObject("professorInfo", professor);
		return mv;
	}
	/**
	 * @description 删除专家信息
	 * @author shy
	 * @date 2016-8-30
	 */	
	@RequestMapping("/deleteProfessorInfo")
	@ResponseBody
	public String deleteProfessorInfo(String professorId,HttpSession session){
		try {
			this.professorService.deleteProfessorInfo(professorId);
			return Constant.DEL_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return Constant.DEL_UN_SUCCESS;
		}
		
	}
	
	/**
	 * @description 结束专家信息录入任务
	 * @author shy
	 * @date 2016-8-30
	 */
	@RequestMapping("/completeTask")
	@ResponseBody
	public String completeTask(String projectId, HttpSession session) {
		// 流程 完成任务
		User u = (User) session.getAttribute("loginUser");
		ProjectInfo pro = projectInfoService.get(ProjectInfo.class, projectId);
		pro.setStatus(Integer.valueOf(ProjectStatus.AUDITED_BEGIN.toString()));
		Task task = workflowProcessInstanceService.findTask(pro.getWorkflowId());
		workflowProcessInstanceService.complete(task.getId(), null);
		return Constant.SAVE_SUCCESS;
	}
}
